package itsm.isperp.module.controller.task;

import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.dto.project.ProjectDomain;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.service.task.TskTaskService;
import itsm.isperp.module.utils.AnalysisQueryUtils;
import itsm.isperp.module.utils.ProjectDomainUtils;
import itsm.isperp.report.Counter;
import itsm.isperp.report.data.warehouse.QueryRequest;
import itsm.isperp.report.resolver.DataResolverUtils;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("task/search")
public class TskTaskSearchController {

	@Autowired
	protected TskTaskService tskTaskService;

	@Autowired
	protected AppDictService appDictService;

	@Autowired
	protected ProjProjectService projProjectService;

	@Autowired
	protected AppUserService appUserService;

	/**
	 * 搜索页面
	 * 
	 * @return
	 */
	@RequestMapping(value = { "manage", "" })
	public ModelAndView manage() {
		ModelAndView mv = new ModelAndView("task/search/manage");

		// 初始化筛选条件
		// 项目
		mv.addObject(
				"projects",
				projProjectService.findAll(
						new JqGridRequest().isPageable(false).setSelectColumns(
								"id", "name")).getContent());
		int dataRange=ProjectDomainUtils.getUserMaxDataRange();
    mv.addObject("dataRange",dataRange);

		return mv;
	}

	/**
	 * 根据分页器获取实体列表的json格式数据
	 * 
	 * @param request
	 *            {@link JqGridRequest} jqGrid数据请求（查询条件、排序）
	 * @param req
	 *            {@link HttpServletRequest} http请求
	 * @return json格式数据
	 */
	@RequestMapping("list")
	@ResponseBody
	public PageResponse<Map<String, Object>> list(JqGridRequest request,
			HttpServletRequest req) {
		request.addFilterRule("code", JqGridFilterRuleOp.isnotnull, null);
		return this.tskTaskService.searchAllByFilter(request);

	}

	@RequestMapping(value = { "analysis", "analysisView" })
	public ModelAndView analysis(JqGridRequest req, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("task/search/analysis");

		return mv;
	}

	@RequestMapping(value = { "analysisList" })
	@ResponseBody
	public PageResponse<Map<String, Object>> analysisList(
			JqGridRequest request, HttpServletRequest req) {

		String projectId = req.getParameter("projectId");
		if (StringUtils.isNotEmpty(projectId)) {
			request.addFilterRule("projectId", JqGridFilterRuleOp.eq, projectId);
		}
		StringBuilder sb = new StringBuilder();

		AnalysisQueryUtils.buildAnalysisQuery(2, sb, req);

		request.setSearchString(" 1=1 " + sb.toString());

		request.addFilterRule("status", JqGridFilterRuleOp.ne, "已关闭");
		request.setProjectDomain(ProjectDomain.newInstance());
		PageResponse<Map<String, Object>> p = this.tskTaskService
				.findAllDetailByRequest(request).toPage();

		return p;
	}

	@RequestMapping(value = { "reloadQueryPanelData" })
	@ResponseBody
	public Map<String, Map<String, Counter>> reloadQueryPanelData(
			JqGridRequest request, HttpServletRequest req) {

		StringBuilder sb = new StringBuilder();
		// Set<String> fields = new HashSet<>();
		// fields.add("confirmtimeman_name");
		// fields.add("supporter_name");
		// fields.add("incident_type");
		// fields.add("priority_name");
		// fields.add("status");
		// fields.add("sla_status");

		AnalysisQueryUtils.buildAnalysisQuery(2, sb, req);

		QueryRequest qr = new QueryRequest();
		 
		qr.addColumn("task_type");
		qr.addColumn("plan_principal_name");
		qr.addColumn("status");

		qr.setFrom("tsk_task");
		qr.setWhere(sb.toString()
				+ " AND status<>'已关闭'"
				+ ProjectDomainUtils.getUserDomainSql(ProjectDomain
						.newInstance()));

		Map<String, Map<String, Counter>> map = DataResolverUtils.resolve(qr);

		return map;
	}
}